package com.applozic.mobicomkit.uiwidgets.conversation.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.applozic.mobicomkit.api.conversation.Message;
import com.applozic.mobicomkit.contact.BaseContactService;
import com.applozic.mobicomkit.ChatConfig;
import com.applozic.mobicomkit.uiwidgets.conversation.ConversationUIService;
import com.applozic.mobicommons.people.contact.Contact;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.GetPacketResultActivity;
import com.lottery.ruok.lottrey.activity.GiveRewardActivity;
import com.lottery.ruok.lottrey.activity.LoginActivity;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.dialog.RedPacketAniDialog;
import com.lottery.ruok.lottrey.eventbusObject.GrabRedPacketSucess;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GrabGiftRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GrabGiftResponse;
import com.lottery.ruok.lottrey.ui.component.BaseRelativeLayout;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;
import glideHelp.GlideHelper;

public class RedPacketRightView extends BaseRelativeLayout implements View.OnClickListener {
    @BindView(R.id.avatar)
    CircleImageView circleImageView;
    @BindView(R.id.descp)
    TextView titles;
    @BindView(R.id.layout)
    LinearLayout layout;
    private Message message;
    private BaseContactService contactService;

    public RedPacketRightView(Context context) {
        super(context);
    }

    public RedPacketRightView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RedPacketRightView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void initView(Context context) {
        setContentView(R.layout.red_packet_item_right);
        setRoundImg(circleImageView);

        setOnClickListener(this);
    }

    @Override
    protected void load(Context context) {

    }

    public void setData(Message message, BaseContactService contactService) {
        this.message = message;
        this.contactService = contactService;
        titles.setText(message.getMetadata().get(ChatConfig.PACKET_MESSAGE));
    }

    private void gotoGivePrize(Message message, Contact contact) {
        if (!UserInfo.getToken().equals("") && contact != null) {
            if (UserInfo.getIsTest() == 1) {
                utils.Utils.showToast("试玩账号不能打赏");
            } else {
                if (!((FragmentActivity) mContext).getIntent().getBooleanExtra("is_single", false)) {//单人聊天不能打赏
                    Intent intent = new Intent(mContext, GiveRewardActivity.class);
                    intent.putExtra("avatar", contact.getImageURL());
                    intent.putExtra("user", message.getContactIds());
                    mContext.startActivity(intent);
                }
            }
        }
    }


    @Override
    public void onClick(View view) {
        if (!UserInfo.getToken().equals("")) {
            if (UserInfo.getIsTest() == 1 && UserInfo.getIsTry() != 1) {
                utils.Utils.showToast(Common.IS_TEST);
            } else {
                if (message == null) {
                    return;
                }
                RetrofitBase.getRetrofit().create(GrabGiftRequest.class).getResult(message.getMetadata().get(ChatConfig.PACKET_ID), UserInfo.getUsername(), UserInfo.getToken())
                        .compose(RxHelper.<GrabGiftResponse>handleResult())
                        .subscribe(new RxSubscribe<GrabGiftResponse>(mContext) {
                            @Override
                            protected void _onNext(GrabGiftResponse grabGiftResponse) {
                                switch (grabGiftResponse.getTypes()) {
                                    case 0:
                                        utils.Utils.showToast(grabGiftResponse.getMsg());
                                        break;
                                    case 1: {
                                        //播放动画，再跳转到红包领取详情页
                                        utils.Utils.showToast(grabGiftResponse.getMsg());

                                        GrabRedPacketSucess grabRedPacketSucess = new GrabRedPacketSucess();
                                        grabRedPacketSucess.setRedPacketFrom(message.getMetadata().get(ChatConfig.PACKET_FROM));
                                        grabRedPacketSucess.setHave_get_packet_id(grabGiftResponse.getGift_rand_id());
                                        grabRedPacketSucess.setRedPacketID(grabGiftResponse.getGift_id());
                                        grabRedPacketSucess.setConversationId(((FragmentActivity) mContext).getIntent().getStringExtra(ConversationUIService.USER_ID));
                                        EventBus.getDefault().post(grabRedPacketSucess);

                                        Intent intent = new Intent(mContext, RedPacketAniDialog.class);
                                        intent.putExtra(ChatConfig.PACKET_GET_ID, grabGiftResponse.getGift_rand_id());
                                        intent.putExtra(ChatConfig.PACKET_ID, grabGiftResponse.getGift_id());
                                        intent.putExtra(ChatConfig.CONVERSATION_ID, ((FragmentActivity) mContext).getIntent().getStringExtra(ConversationUIService.USER_ID));
                                        mContext.startActivity(intent);
                                        break;
                                    }
                                    case 2: {
                                        //跳转到红包领取详情页
                                        utils.Utils.showToast(grabGiftResponse.getMsg());

                                        Intent intent = new Intent(mContext, GetPacketResultActivity.class);
                                        intent.putExtra(ChatConfig.PACKET_GET_ID, grabGiftResponse.getGift_rand_id());
                                        intent.putExtra(ChatConfig.PACKET_ID, grabGiftResponse.getGift_id());
                                        mContext.startActivity(intent);
                                        break;
                                    }
                                    case 3:
                                        utils.Utils.showToast(grabGiftResponse.getMsg());
                                        break;
                                }
                            }

                            @Override
                            protected boolean showDialog() {
                                return false;
                            }
                        });
            }
        } else {
            Intent intent = new Intent(mContext, LoginActivity.class);
            mContext.startActivity(intent);
            ((Activity) mContext).overridePendingTransition(R.anim.anim_enter_from_bottom, R.anim.activity_stay);
        }

    }

    private void setRoundImg(ImageView myAvatar) {
        if (myAvatar != null) {
            if (UserInfo.getToken().equals("")) {
                myAvatar.setImageResource(R.mipmap.chat_default);
            } else {
                if (UserInfo.getAvatar().equals("")) {
                    myAvatar.setImageResource(R.mipmap.chat_default);
                } else {
                    GlideHelper.loadRoundImage(getContext(), UserInfo.getAvatar(), myAvatar);
                }
            }
        }
    }
}
